<template>
  <el-dialog :title="info.title" v-if="visible" append-to-body :visible.sync="visible" center width="460px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="100px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="24">
          <el-form-item label="流程名称" prop="name">
            <el-input v-model="form.name" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="上级主管" prop="senior_role">
            <el-select v-model="form.senior_role" size="small" style="width: 100%">
              <el-option
                v-for="item in states"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :style="'display:'+ (form.senior_role === 1 ? 'none' : '')" :span="24">
          <el-form-item label="审批部门" :prop="form.senior_role === 1 ? '' : 'dept_id'">
            <DeptSelect v-model="form.dept_id" />
          </el-form-item>
        </el-col>
        <el-col :style="'display:'+ (form.senior_role === 1 ? 'none' : '')" :span="24">
          <el-form-item label="审批职位" :prop="form.senior_role === 1 ? '' : 'pos_id'">
            <PositionSelect v-model="form.pos_id" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="起始节点" prop="begin_node">
            <el-select v-model="form.begin_node" size="small" style="width: 100%">
              <el-option
                v-for="item in states"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="结束节点" prop="end_node">
            <el-select v-model="form.end_node" size="small" style="width: 100%">
              <el-option
                v-for="item in states"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :style="'display:'+ (form.end_node === 1 ? 'none' : '')" :span="24">
          <el-form-item label="下个流程" :prop="form.end_node === 1 ? '' : 'next_id'">
            <el-select v-model="form.next_id" size="small" style="width: 100%">
              <el-option
                v-for="item in nodes"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleEdit()">保 存</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { selNode, updProcessInfo, addProcessInfo } from '@/api/process'
import DeptSelect from '@/myComponents/DeptSelect'
import PositionSelect from '@/myComponents/PositionSelect'

export default {
  components: { DeptSelect, PositionSelect },
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      form: {},
      nodes: [],
      states: [{ key: 0, value: '否' }, { key: 1, value: '是' }],
      rules: {
        name: [{ required: true, trigger: 'blur', message: '请填写名称' }],
        senior_role: [{ required: true, trigger: 'blur', message: '请选择是否上级主管审批' }],
        dept_id: [{ required: true, trigger: 'blur', message: '请选择审批部门' }],
        pos_id: [{ required: true, trigger: 'blur', message: '请选择审批职位' }],
        next_id: [{ required: true, trigger: 'blur', message: '请选择下个流程' }],
        begin_node: [{ required: true, trigger: 'blur', message: '请选择截至节点' }],
        end_node: [{ required: true, trigger: 'blur', message: '请选择开始节点' }]
      },
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.form = this.info.edit
        this.selNode()
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    selNode(){
      selNode({
        'lap_id': this.form.lap_id
      }).then(response => {
        this.nodes = response.list
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    handleEdit() {
      this.$refs['formDate'].validate(async(valid) => {
        if (valid) {
          let sort = 20
          if(this.form.begin_node === 1) sort = 0
          if(this.form.end_node === 1) {
            sort = 99
            this.$set(this.form, 'next_id', '')
          }
          this.form.sort = sort
          this.form.begin_node += ''
          this.form.end_node += ''
          this.form.senior_role += ''
          if (this.info.type === 'add') {
            addProcessInfo(this.form).then(response => {
              this.getData()
              this.handleCancel()
              this.$message.success('添加成功')
            }).catch(err => {
              this.$message.error(err.msg)
            })
          } else {
            updProcessInfo(this.form).then(response => {
              this.getData()
              this.handleCancel()
              this.$message.success('编辑成功')
            }).catch(err => {
              this.$message.error(err.msg)
            })
          }
        }
      })
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style>

</style>
